package com.abigdreamer.swing.app;

import com.abigdreamer.swing.app.skin.SkinConst;
import craky.util.Config;
import java.util.ArrayList;
import java.util.List;

public class LoginConfigMgr
{
  public static final String FILE_SEP = System.getProperty("file.separator");
  public static final String USER_DIR = System.getProperty("user.dir");
  public static final String DATA_DIR = USER_DIR + FILE_SEP + "data";
  public static final String BANNER_BG_DIR = SkinConst.IMAGE_DIR + FILE_SEP + "banner_bg";
  public static final String CONFIG_DIR = USER_DIR + FILE_SEP + "config";
  public static final String LOGIN_CONFIG_PATH = CONFIG_DIR + FILE_SEP + "login_config.properties";
  private static List<String> loginHistory = new ArrayList(10);
  private static Config loginConfig = new Config(LOGIN_CONFIG_PATH);
  public static final int MAX_HISTORY_USER_COUNT = 10;
  public static final String LOGIN_HISTORY = "Login_History";
  public static final String USER_LIST_SEP = ",";
  public static final String AUTO_LOGIN_KEY = ".auto";
  public static final String PASSWORD_KEY = ".password";
  
  public static boolean isAllowedName(String name)
  {
    return (name != null) && (!(name = name.trim()).isEmpty()) && (name.indexOf(',') < 0);
  }
  
  public static void loadLoginHistory()
  {
    String history = loginConfig.getProperty("Login_History");
    
    int count = 0;
    String[] historyList;
    if ((history != null) && ((historyList = history.split(",")).length > 0)) {
      for (String name : historyList)
      {
        if (isAllowedName(name))
        {
          loginHistory.add(name);
          count++;
        }
        if (count >= 10) {
          break;
        }
      }
    }
  }
  
  public static List<String> getLoginHistory()
  {
    return loginHistory;
  }
  
  public static void addHistoryUser(String name)
  {
    loginHistory.add(name);
  }
  
  public static boolean checkUserIsAutoLogin(String first)
  {
    return Boolean.parseBoolean(loginConfig.getProperty(first + ".auto", "false"));
  }
  
  public static String getPassword(String first)
  {
    return loginConfig.getProperty(first + ".password");
  }
  
  public static void saveUser(String name, String password, boolean savePassword, boolean autoLogin)
  {
    loginHistory.remove(name);
    loginHistory.add(0, name);
    loginConfig.remove(name + ".auto");
    loginConfig.remove(name + ".password");
    if (savePassword) {
      loginConfig.setPropertie(name + ".password", password);
    }
    if (autoLogin) {
      loginConfig.setPropertie(name + ".auto", String.valueOf(autoLogin));
    }
    if (loginHistory.size() > 10) {
      loginHistory = loginHistory.subList(0, 10);
    }
    loginConfig.setPropertie("Login_History", getHistoryUserString());
    loginConfig.saveConfig();
  }
  
  public static String getHistoryUserString()
  {
    return loginHistory.toString().replaceAll("\\[|\\]| ", "");
  }
  
  public static void removeHistoryUser(String name)
  {
    loginHistory.remove(name);
    loginConfig.remove(name + ".password");
    loginConfig.remove(name + ".auto");
    loginConfig.setPropertie("Login_History", getHistoryUserString());
    loginConfig.saveConfig();
  }
  
  public static Config getLoginConfig()
  {
    return loginConfig;
  }
}
